package pay.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import pay.entity.MessageEntity;
import pay.entity.vo.MessageFeedbackUnionVo;

import java.util.List;

public interface MessageMapper extends BaseMapper<MessageEntity> {

    List<MessageFeedbackUnionVo> selectMessageUnionFeedback(Page<MessageFeedbackUnionVo> page, @Param("userId") String userId);

    List<MessageEntity> selectByUserAndIsRead(Page<MessageEntity> page, @Param("userId") String userId, @Param("messageType") Integer messageType);

    @Update("UPDATE message SET is_read = 1, read_time = NOW() WHERE id = #{id}")
    int markAsRead(@Param("id") Integer id);

    @Select("select count(id) from message where user_id=#{userId} and is_read=#{isRead} and `type`=#{type}")
    int selectCount(@Param("userId") String userId, @Param("isRead") int isRead, @Param("type") int type);

    @Select("select * from message where user_id=#{user_id} and `type`=#{type} order by id desc limit 1")
    MessageEntity selectLastMessage(@Param("user_id") String userId, @Param("type") int type);

    // 聚合查询消息和反馈的最后一条
    MessageFeedbackUnionVo selectLastMessageUnionFeedback(
            @Param("userId") String userId
    );


}
